<?php
class Admin_Answer extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('answer_m');
		$this->load->model('categories_m');
	}
	public function index($cat_id = NULL){
		
		if(!empty($cat_id)){
			$this->action = 'new|'.site_url('admin/polls/answer/create/'.$cat_id).'|'.lang('global:addnew').'+ remove|javascript:void(0)|'.lang('global:delete_selected');
			$this->db->where('cat_id',$cat_id);
		}
		$model = $this->answer_m->get_all();
		

		
		
		$cat = $this->categories_m->get_all();
		$cat_select = array_for_select($cat,'id','title');
		
		
		$this->template
			->set_breadcrumb(lang('polls:polls'))
			->set('action',$this->action)
			->set('model',$model)
			->set('cat_select',$cat_select)
			->set('cat_id',$cat_id)
			->set('help',lang('polls:help'))
			->set('url',site_url($this->uri->uri_string()))
			->build($this->controller.'/'.$this->method);
	}
	
	public function create($cat_id = NULL){
		
		
		$model = $this->input->post();
		
		$this->form_validation->set_rules('cat_id', 'lang:polls:question', 'required');
		$this->form_validation->set_rules('title', 'lang:polls:answer', 'required');
		
		
		$type =  substr($this->input->post('btnAction'), 0);
		
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); 
					}
				}
				$id_insert = $this->answer_m->insert($model);
				if($type == 'save')
					redirect('admin/'.$this->module.'answer');
				if($type == 'add')
					redirect('admin/'.$this->module.'/answer/create/'.$cat_id);
				if($type == 'edit')
					redirect('admin/'.$this->module.'/answer/edit/'.$id_insert);
			}
			
		}
		$cat = $this->categories_m->get_all();
		$cat_select = array_for_select($cat,'id','title');
//		$model['cat_id'] = $cat_id ? $cat_id : NULL;
		$this->template
			->set_breadcrumb(lang('polls:edit_answer'));
		$this->view(array('model'=>$model,'cat_select'=>$cat_select,'cat_id'=>$cat_id));
	}
	
	public function edit($id){
		
		$model =(array)$this->answer_m->get($id);
		
		$this->form_validation->set_rules('cat_id', 'lang:polls:question', 'required');
		$this->form_validation->set_rules('title', 'lang:polls:answer', 'required');
		
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$model = $this->input->post();
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); 
					}
				}
				$this->answer_m->update($id,$model);

				if($type == 'save')
					redirect('admin/'.$this->module.'/answer');
				if($type == 'add')
					redirect('admin/'.$this->module.'/answer/create/'.$model['cat_id']);
				if($type == 'edit')
					redirect('admin/'.$this->module.'/answer/edit/'.$id);
			}
			
		}
		$cat = $this->categories_m->get_all();
		$cat_select = array_for_select($cat,'id','title');
		
		$this->template
			->set_breadcrumb(lang('polls:add_answer'));
		$this->view(array('model'=>$model,'cat_select'=>$cat_select));
	}
	
	function ordering(){
		$model = $this->input->post();
		$id = $model['id'];
		unset($model['id']);
		$this->answer_m->ordering($id,$model);
	}
	
	function delete($id){
		$this->answer_m->delete($id);
	}
}